#include "capturepage.h"
#include "ui_capturepage.h"
#include <QTimer>
#include <QTextEdit>
#include <QGroupBox>

CapturePage::CapturePage(const QString &interfaceName,QWidget *parent)
    :QWidget(parent),
    ui(new Ui::CapturePage)
{
    interface = interfaceName;
    ui->setupUi(this);


    setButtonQSS(ui->backButton);
    setButtonQSS(ui->captureButton);
    setButtonQSS(ui->stopButton);
    ui->interfaceLabel->setText(interfaceName);

    //binding buttons
    connect(ui->stopButton,&QPushButton::clicked,this,&CapturePage::stopCapture);
    connect(ui->backButton,&QPushButton::clicked,this,&CapturePage::backToPrevPage);
    connect(ui->captureButton,&QPushButton::clicked,this,&CapturePage::startCapture);
    capturePacket = new CapturePacket();

    //scroll area
    scrollContent = new QWidget();
    scrollLayout = new QVBoxLayout(scrollContent);
    scrollLayout->setAlignment(Qt::AlignTop);

    QScrollArea *scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    scrollArea->setWidget(scrollContent);

    //erase verticalLayout
    QLayoutItem* child ;
    while((child = ui->verticalLayout->takeAt(0))!=nullptr){
        delete child->widget();
        delete child;
    }
    ui->verticalLayout->addWidget(scrollArea);


}

void CapturePage::setButtonQSS(QPushButton* btn){
    btn->setFixedSize(120,50);
    btn->setStyleSheet(
        "QPushButton {"
        "   color: rgb(135, 96, 16);"
        "   background-color: rgb(0, 0, 0);"
        "   border: 2px solid rgb(135, 96, 16);"
        "   border-radius: 10px;"
        "   padding: 10px;"
        "   margin: -1px;"
        "}"
        "QPushButton:hover {"
        "   background-color: rgb(30, 30, 30);"
        "}"
        );
}

void CapturePage::backToPrevPage(){
    if(capturePacket){
        capturePacket->stopCapture();
    }

    QTimer::singleShot(100,this,[this](){
            emit requestBack();
            this->hide();
        });
}

void CapturePage::startCapture(){
    // if(isCapturing || stopRequested){
    //     return;
    // }
    // isCapturing = true;
    // stopRequested = false;

    ui->stopButton->setEnabled(true);
    ui->captureButton->setEnabled(false);

    if(capturePacket){
        capturePacket->startCapture(interface);
        connect(capturePacket->myPacket,&MyPacket::captured,this,&CapturePage::onPacketCaptured);
    }
}

void CapturePage::stopCapture(){
    // if(!isCapturing){
    //     return;
    // }

    // stopRequested = true;
    ui->captureButton->setEnabled(true);
    ui->stopButton->setEnabled(false);

    if(capturePacket){
        capturePacket->stopCapture();
    }

    // isCapturing = false;
}

void CapturePage::onPacketCaptured(const QString &time,const QString &from ,const QString &to,
                      const QString &length,const QString &protocol ,const QString &info){
    QGroupBox *packetBox = new QGroupBox(scrollContent);
    packetBox->setStyleSheet(
        "QGroupBox {"
        "   border: 1px solid #3a3a3a;"
        "   border-radius: 4px;"
        "   margin-top: 6px;"
        "   padding: 4px;"
        "}"
        "QGroupBox::title {"
        "   color: #d7d7d7;"
        "   subcontrol-origin: margin;"
        "   left: 8px;"
        "}"
        );

    // 设置标题
    QString title = QString("%1 | %2 | %3 bytes").arg(time, protocol, length);
    packetBox->setTitle(title);

    // 创建内部布局
    QVBoxLayout *innerLayout = new QVBoxLayout(packetBox);
    innerLayout->setContentsMargins(8, 20, 8, 8);

    // 添加源/目标信息
    QLabel *fromToLabel = new QLabel(QString("<b>From:</b> %1  <b>To:</b> %2").arg(from, to));
    fromToLabel->setStyleSheet("color: #b1b1b1;");

    // 添加详细信息
    QTextEdit *infoText = new QTextEdit();
    infoText->setText(info);
    infoText->setReadOnly(true);
    infoText->setFrameShape(QFrame::NoFrame);
    infoText->setStyleSheet(
        "QTextEdit {"
        "   background-color: #1e1e1e;"
        "   color: #d7d7d7;"
        "   border: none;"
        "}"
        );

    innerLayout->addWidget(fromToLabel);
    innerLayout->addWidget(infoText);

    // 添加到滚动区域
    scrollLayout->addWidget(packetBox);


    // 限制显示包数量
    const int maxPackets = 500;
    if (scrollLayout->count() > maxPackets) {
        QLayoutItem *item = scrollLayout->takeAt(0);
        if (item && item->widget()) {
            item->widget()->deleteLater();
        }
        delete item;
    }
}

CapturePage::~CapturePage()
{
    stopCapture();
    if(capturePacket) {
        delete capturePacket;
        capturePacket = nullptr;
    }
    delete ui;
}
